<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.userName" placeholder="请输入用户名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-select v-model="search.status" placeholder="请选择状态" clearable>
						<el-option label="正常" value="0"></el-option>
						<el-option label="退群" value="1"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
				<el-form-item>
					<el-button v-auth="'group:user:add'" type="primary" icon="el-icon-document-add" @click="onOpenAddUser()" :disabled="!search.groupId">添加</el-button>
					<el-button v-auth="'group:user:edit'" type="primary" icon="el-icon-edit" @click="onOpenEditUser()" :disabled="tableData.selections.length != 1">修改</el-button>
					<el-button v-auth="'group:user:remove'" type="primary" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">退群</el-button>
				</el-form-item>
			</el-form>
			<el-table ref="tableUserRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
				<el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="登录账号" prop="loginName" min-width="150" show-overflow-tooltip>
					<template #default="scope">
						<span>
							{{scope.row.loginName}}
							<el-tag v-if="scope.row.userPerm == 1" class="ml-2" type="danger">群主</el-tag>
							<el-tag v-else-if="scope.row.userPerm == 2"  class="ml-2" type="success">管理员</el-tag>
							<el-tag v-else class="ml-2" type="warning">成员</el-tag>
						</span>
					</template>
				</el-table-column>
				<el-table-column label="成员昵称" prop="userName" min-width="100" show-overflow-tooltip></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else class="color-info">退群</span>
					</template>
				</el-table-column>
				<el-table-column label="入群时间" prop="createTime" width="150" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="125"
					v-auths="['group:user:edit', 'group:user:remove']">
					<template #default="scope">
						<el-button v-auth="'group:user:edit'" type="primary" link @click="onOpenEditUser(scope.row)">修改</el-button>
						<el-button v-auth="'group:user:remove'" type="primary" link @click="onTabelRowDel(scope.row)">退群</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onPageSizeChange"
				@current-change="onPageCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 40, 50, 60]"
				v-model:current-page="search.pageNum"
				background
				v-model:page-size="search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<EditUserGroup ref="editUserGroupRef" @submit="onTableSearch"/>
		<AddUserGroup ref="addUserGroupRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, watch } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddUserGroup from '/@/views/system/group/addUserGroup.vue';
import EditUserGroup from '/@/views/system/group/editUserGroup.vue';
import { getUserGroupList, delUserGroup } from '/@/api/system/group/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
	name: 'group/user/list',
	props: {
		// 绑定值
		model: {
			type: Object,
			default: () => {},
		},
	},
	components: { AddUserGroup, EditUserGroup },
	setup(props) {
		const tableUserRef = ref();
		const addUserGroupRef = ref();
		const editUserGroupRef = ref();
		const state = reactive({
			search:{
				groupId: undefined,
				userName: undefined,
				status: '0',
				startTime: undefined,
				endTime: undefined,
				orderBy: 'createTime',
				isAsc: 'desc',
				pageNum: 1,
				pageSize: 10
			},
			dateData: {
				value: [],
				shortcuts: getDateRangeShortcuts()
			},
			tableData: {
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			if(state.search.groupId){
				getUserGroupList(Object.assign(state.search, params)).then((res) => {
					if(res.code == 0){
						state.tableData.data = res.data;
						state.tableData.total = res.total;
						tableUserRef.value.clearSelection();
					}
				});
			}else{
				state.tableData.data = [];
				state.tableData.total = 0;
				tableUserRef.value.clearSelection();
			}
		}

		//查询
		const onTableSearch = () => {
			if(state.dateData.value.length == 2){
				state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
				state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
			}
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.dateData.value = [];
			state.search.userName = undefined;
			state.search.status = undefined;
			state.search.startTime = undefined;
			state.search.endTime = undefined;
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 打开新增用户弹窗
		const onOpenAddUser = (row: object) => {
			addUserGroupRef.value.openDialog({groupId: state.search.groupId});
		};
		// 打开编辑用户弹窗
		const onOpenEditUser = (row: object) => {
			row = row || state.tableData.selections[0];
			editUserGroupRef.value.openDialog(row);
		};
		// 退群选中行
		const onTabelRowsDel = () => {
			const ids = state.tableData.selections.map(item => item.id);
			if(ids && ids.length > 0){
				ElMessageBox.confirm('确定将选中的用户移出群吗？', '提示', {
					confirmButtonText: '退群',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					delUserGroup(ids).then((res) => {
						if(res.code === 0){
							ElMessage.success('退群成功');
							loadTableData();
						}
					});
				}).catch(() => {});
			}
		};
		// 退群当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定将该【' + row.userName + '】用户移出群吗？', '提示', {
				confirmButtonText: '退群',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delUserGroup([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('退群成功');
						loadTableData();
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData();
		});
		//监听model数据
		watch(() => props.model.id, () => {
			state.search.groupId = props.model.id;
			loadTableData();
		}, {deep: true});
		
		return {
			tableUserRef,
			addUserGroupRef,
			editUserGroupRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onOpenAddUser,
			onOpenEditUser,
			onTabelRowsDel,
			onTabelRowDel,
			...toRefs(state),
		};
	},
};
</script>